<template>
  <div class="page-user-rights">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        label-width="auto"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="rightsName"
            :label="$t('user.rightsName')+':'"
          >
            <el-input
              v-model="searchForm.rightsName"
              type="text"
              :placeholder="$t('user.rightsName')"
            />
          </el-form-item>
          <el-form-item
            prop="serviceType"
            :label="$t('user.rightsType')+':'"
          >
            <el-select
              v-model="searchForm.serviceType"
              :placeholder="$t('user.rightsType')"
            >
              <el-option
                :label="$t('user.sysRights')"
                :value="0"
              />
              <el-option
                :label="$t('user.selfRights')"
                :value="1"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch(searchFormRef)"
            >
              {{ $t('shop.resetMap') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <div class="operation-bar">
        <div
          v-if="isAuth('user:userRights:update')"
          class="default-btn primary-btn"
          @click="onAddOrUpdate()"
        >
          {{ $t('userRights.customBenefits') }}
        </div>
      </div>
      <!-- 表格 -->
      <div class="table-con user-rights-table">
        <el-table
          ref="userRightsTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%"
        >
          <el-table-column
            type="index"
            :label="$t('number')"
            width="80"
          />
          <el-table-column
            prop="rightsName"
            :label="$t('user.rightsName')"
            width="auto"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.rightsName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="icon"
            :label="$t('user.rightsIcon')"
            width="auto"
          >
            <template #default="scope">
              <div class="table-cell-image">
                <img-show
                  :src="resourcesUrl + scope.row.icon"
                  :alt="scope.row.rightsName"
                />
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="description"
            :label="$t('user.rightsDescription')"
            width="auto"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.description }}</span>
            </template>
          </el-table-column>

          <el-table-column
            prop="serviceType"
            :label="$t('user.rightsType')"
          >
            <template #default="scope">
              <span class="tag-text">{{ scope.row.serviceType===0 ? $t('user.sysRights') : $t('user.selfRights') }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="seq"
            :label="$t('temp.sequence')"
            width="auto"
          />
          <el-table-column
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
            width="auto"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('user:userRights:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row.rightsId)"
                >
                  {{ $t('groups.edit') }}
                </div>
                <div
                  v-if="scope.row.serviceType === 1"
                  class="default-btn text-btn"
                  @click="onDelete(scope.row.rightsId)"
                >
                  {{ $t('text.delBtn') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>

    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import AddOrUpdate from './add-or-update.vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { isAuth } from '@/utils/index'
const resourcesUrl = import.meta.env.VITE_APP_RESOURCES_URL
const dataListLoading = ref(false)
const dataList = ref([])
let tempSearchForm = null // 保存上次点击查询的请求条件
const page = ref({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = ref({
  rightsName: '',
  serviceType: ''
}) // 搜索
const onGetDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm.value))
  }
  http({
    url: http.adornUrl('/user/userRights'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.value.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.value.pageSize : pageParam.pageSize
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.value.total = data.total
    dataListLoading.value = false
  })
}

onMounted(() => {
  onGetDataList(page.value)
})

// 新增 / 修改
const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)
const onAddOrUpdate = (id) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}
const onDelete = (id) => {
  ElMessageBox.confirm($t('seckill.isDeleOper'), $t('remindPop.remind'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/user/userRights/' + id),
      method: 'delete',
      data: http.adornData({})
    }).then(() => {
      ElMessage({
        message: $t('remindPop.succeeded'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  }).catch(() => { })
}
/**
 * 刷新回调
 */
const refreshChange = () => {
  onGetDataList(page.value)
}
const onSearch = (newData = false) => {
  page.value.currentPage = 1
  onGetDataList(page.value, newData)
}

/**
 * 重置表单
 * @param {String} formName 表单名称
 */
const searchFormRef = ref(null)
const onResetSearch = (formName) => {
  formName.resetFields()
}

const onPageSizeChange = (val) => {
  page.value.pageSize = val
  onGetDataList()
}
const onPageChange = (val) => {
  page.value.currentPage = val
  onGetDataList()
}

</script>
